package cn.gong.store.async;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

import cn.gong.store.db.callback.DBCallBack;
import cn.gong.store.db.callback.DBDeal;

public class AsyncData {
	private byte[] lock = new byte[0];
	private static AsyncData instance = new AsyncData();
	private ThreadPoolExecutor executor;

	private AsyncData() {
		executor = new ThreadPoolExecutor(5, 8, 180, TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(10), new ThreadPoolExecutor.DiscardOldestPolicy());
	}

	public static AsyncData getInstance() {
		return instance;
	}

	public void deal(final String name, final DBCallBack callBack, final DBDeal deal) {
		executor.execute(new Runnable() {
			public void run() {
				synchronized (lock) {
					Object data = deal.deal();
					if (callBack != null) {
						callBack.dbTaskFinish(name, data);
					}
				}
			}
		});
	}
}
